package model;

import java.util.ArrayList;
import java.util.Iterator;

import model.trajectory.TargetModel;

import utils.Pair;
import utils.Point3D;
import utils.TimeIntervalle;
import utils.Vector3D;


public class BaseModel
{
	private final int ID;
	private String name;
	public Point3D position;
	
	public ArrayList<DoorModel> doors;
	public ArrayList<AdjacencyModel> adjacencies;
	
	public ArrayList<Pair<TimeIntervalle, ArrayList<TargetModel>>> targetsByIntervalle;

	public boolean isSelected = false;
	private boolean isHighlighted = false;
	private boolean hasChanged;
			
	public BaseModel(int ID)
	{
		this.ID = ID;
		this.position = new Point3D();
		this.name = "";
	}
	
	public BaseModel(int ID, String name)
	{
		this.ID = ID;
		this.setName(name);
		this.position = new Point3D();
		
		this.doors = new ArrayList<DoorModel>();
		this.adjacencies = new ArrayList<AdjacencyModel>();
	}
	

	public void addTarget(TimeIntervalle intervalle, TargetModel target)
	{
		for(Iterator<Pair<TimeIntervalle, ArrayList<TargetModel>>> it = this.targetsByIntervalle.iterator(); it.hasNext();)
		{
			Pair<TimeIntervalle, ArrayList<TargetModel>> pair = it.next();
			if(pair.key.equals(intervalle))
			{
				int index = 0;
				for(Iterator<TargetModel> itTarget = pair.value.iterator(); itTarget.hasNext();)
				{
					TargetModel currentTarget = itTarget.next();
					if(currentTarget.getID() <= target.getID())
					{
						index++;
					}
				}
				pair.value.add(index, target);
			}
		}
	}
	
	@Override
	public String toString()
	{
		return "Room: " + getID();
	}

	public ArrayList<TargetModel> targetsByTime(long time) 
	{
		ArrayList<TargetModel> result = new ArrayList<TargetModel>(); 
		for(Iterator<Pair<TimeIntervalle, ArrayList<TargetModel>>> it = this.targetsByIntervalle.iterator(); it.hasNext();)
		{
			Pair<TimeIntervalle, ArrayList<TargetModel>> pair = it.next();
			if(pair.key.include(time))
			{
				return pair.value;
			}
		}
		return result;
	}

	public ArrayList<TargetModel> targetsByIntervalle(TimeIntervalle intervalle) 
	{
		ArrayList<TargetModel> result = new ArrayList<TargetModel>(); 
		for(Iterator<Pair<TimeIntervalle, ArrayList<TargetModel>>> it = this.targetsByIntervalle.iterator(); it.hasNext();)
		{
			Pair<TimeIntervalle, ArrayList<TargetModel>> pair = it.next();
			if(pair.key.equals(intervalle))
			{
				return pair.value;
			}
		}
		return result;
	}

	public Point3D getPosition() {
		return this.position;
	}


	public void translate(Vector3D vector) {
		// TODO Auto-generated method stub
		this.setHasChanged(true);
		this.position = Point3D.sum(this.position, vector);
	}
	
	public void setHasChanged(boolean b) {
		this.hasChanged = b;
	}


	public boolean hasChanged()
	{
		return this.hasChanged;
	}


	public int getID() {
		return ID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isHighlighted() {
		return isHighlighted;
	}

	public void setHighlighted(boolean isHighlighted) {
		this.isHighlighted = isHighlighted;
	}
}
